package com.sddm.printServer.coder;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.sddm.printServer.entiy.Print;


import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;
import java.util.ArrayList;
import java.util.List;

/**
 * @author cjc
 * @version 1.0
 * @date 2022/4/8 16:28
 * @Description
 */
public class PrintDecoder implements Decoder.Text<List<Print>> {
    @Override
    public List<Print> decode(String print)  {
        List<Print> list = new ArrayList<>();
        JSONObject jsonObject = JSONUtil.parseObj(print);
        int total = Integer.parseInt(jsonObject.getStr("total"));
        for (int i = 0; i < total; i++) {
            list.add(JSONUtil.toBean(jsonObject.getStr(String.valueOf(i)),Print.class));
        }
        return list;
    }

    @Override
    public boolean willDecode(String s) {
        return true;
    }

    @Override
    public void init(EndpointConfig endpointConfig) {

    }

    @Override
    public void destroy() {

    }
}
